package com.example.test_1;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.test_1.databinding.ActivityDemoFindBidBinding;
import com.example.test_1.databinding.ActivitySqliteBinding;


public class SqliteActivity extends AppCompatActivity {

    DatabaseHelper databaseHelper;

    private SQLiteDatabase db;

    private ActivitySqliteBinding binding;

    private TextView textView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);


            //定义好变量后，初始化变量
            binding = ActivitySqliteBinding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());


        DatabaseHelper databaseHelper = new DatabaseHelper(this);
         db = databaseHelper.getWritableDatabase();
    }


    //新增用户
    public void addUser(View view) {
        String name = binding.nameEt.getText().toString();
        String phone = binding.phoneEt.getText().toString();
        if (name.isEmpty()||phone.isEmpty()){
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //自己创建一个ContentValues
        ContentValues values = new ContentValues();
        //数据放进去
        values.put("name",name);
        values.put("phone",phone);
        db.insert("user",null,values);

        //清空输入框
        binding.nameEt.setText("");
        binding.phoneEt.setText("");
        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
    }

    //查询用户
    @SuppressLint("Range")
    public void queryUser(View view) {
        binding.contentTv.setText("");
        //查询所有用户
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        if (cursor.moveToFirst()){
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                binding.contentTv.append("姓名：" + name + "  电话：" + phone +"\n");
                /*System.out.println("姓名："+name + "电话：" + phone);*/
            }while (cursor.moveToNext());
        }
    }

    public void updateUser(View view) {
        //根据姓名更新电话
        String name = binding.nameEt.getText().toString();
        String phone = binding.phoneEt.getText().toString();
        if (name.isEmpty()||phone.isEmpty()){
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        ContentValues values = new ContentValues();
        values.put("phone",phone);
        db.update("user",values,"name=?",new String[]{name});

        //清空输入框
        binding.nameEt.setText("");
        binding.phoneEt.setText("");
        Toast.makeText(this, "修改成功！！！", Toast.LENGTH_SHORT).show();
    }

    public void deleteUser(View view) {
        //根据姓名删除
        String name = binding.nameEt.getText().toString();
        String phone = binding.phoneEt.getText().toString();
        if (name.isEmpty()){
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        db.delete("user","name=?",new String[]{name});

        //清空输入框
        binding.nameEt.setText("");
        binding.phoneEt.setText("");
        Toast.makeText(this, "删除成功！！！", Toast.LENGTH_SHORT).show();
    }
}